/**
  ******************************************************************************
  * @file    User/tk_uart.c
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    05-18-2022
  * @brief   TK uart program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "tk_define.h"

/** @addtogroup Template_Project
  * @{
  */

#define TK_UART_SEL  UART1_TX_PA1_RX_PA0


#ifdef TK_UART_SEL

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/

#define TK_UART_A_B   ((TK_UART_SEL & BIT(TK_UART_BIT_UART0_1)) == 0?  UART0 : UART1)


/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

void tk_uart_init()
{
    TYPE_ENUM_TK_UART_SEL temp = TK_UART_SEL;
    u16 tx_pin =  temp & 0xcd5f;
    u16 rx_pin =  (temp >> 16 )& 0xcd5f;
    
    u8 tx_gpio_af = (temp & BIT(TK_UART_BIT_AF)) >> TK_UART_BIT_AF;
    u8 rx_gpio_af = ((temp >> 16 ) & BIT(TK_UART_BIT_AF)) >> TK_UART_BIT_AF;

    u8 tx_gpio = (temp & BIT(TK_UART_BIT_PA_B)) >> TK_UART_BIT_PA_B;
    u8 rx_gpio = ((temp >> 16 ) & BIT(TK_UART_BIT_PA_B)) >> TK_UART_BIT_PA_B;
    
    
    GPIO_TypeDef *pgpio[2] = {GPIOA, GPIOB};
    UART_TypeDef *puart = (temp & BIT(TK_UART_BIT_UART0_1)) == 0?  UART0 : UART1;
    
    ll_gpio_af_sel(pgpio[tx_gpio], tx_pin, (TYPE_ENUM_LL_GPIO_AF)tx_gpio_af);
    ll_gpio_af_sel(pgpio[rx_gpio], rx_pin, (TYPE_ENUM_LL_GPIO_AF)rx_gpio_af);
    
    ll_gpio_set_mode(pgpio[tx_gpio], tx_pin, LL_GPIO_MODE_AF);
    ll_gpio_set_mode(pgpio[rx_gpio], rx_pin, LL_GPIO_MODE_AF);
    

   ll_uart_baudrate_set(puart, LL_UARTx_BAUDRATE_115200);
   
   puart->STA = 0xFFFFFFFF;
   puart->CON = LL_UARTX_CON_UART_EN;
   
}

#define TK_DEBUG_PC 0
#if TK_DEBUG_PC
u16 tk_buf_debug[(26+1) *16];
void tk_debug_connect_pc()
{
    tk_uart_init();
    tk_debug(TK_UART_A_B,tk_buf_debug);
}
#endif

u32 tk_debug_process()
{
    u32 tk_value_temp =tk_process();
    tk_debug(TK_UART_A_B,NULL);
    return tk_value_temp;
}

#endif //TK_UART_SEL

/**
  * @}
  */

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
